﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Installer.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Installer class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Server;
    using Properties;

    /// <summary>
    /// Defines the Installer class.
    /// </summary>
    public static class Installer
    {
        /// <summary>
        /// Installs the underlying database structure.
        /// </summary>
        /// <param name="useAdmin">Whether or not to use the administrator account to access the SQL database.</param>
        /// <param name="userName">The database owner username.</param>
        /// <param name="password">The database owner password.</param>
        /// <param name="databaseName">The database name.</param>
        /// <param name="databaseServer">The database server.</param>
        /// <param name="loginName">The username of the SQL Login to create, if one is to be created.</param>
        /// <param name="loginPassword">The password of the SQL Login to create, if one is to be created.</param>
        public static void Install(bool useAdmin, string userName, string password, string databaseName, string databaseServer, string loginName, string loginPassword)
        {
            string addAccount = string.Empty;
            if (!useAdmin)
            {
                addAccount = @"
CREATE LOGIN [" + loginName + @"] WITH PASSWORD = '" + loginPassword + @"'
GO
EXEC('CREATE USER [" + loginName + @"] FOR LOGIN [" + loginName + @"] WITH DEFAULT_SCHEMA = Lidocaine');
ALTER LOGIN [" + loginName + @"] WITH DEFAULT_DATABASE = [" + databaseName + @"];
EXEC sys.sp_addrolemember @rolename=N'LidocaineBots', @membername=N'" + loginName + @"'
GO
";
            }

            using (SqlConnection conn = new SqlConnection(string.Format(CultureInfo.InvariantCulture, "Data Source={0};Initial Catalog={1};User ID={2};Password={3};", new object[] { databaseServer, databaseName, userName, password })))
            {
                conn.Open();
                Server server = new Server(new ServerConnection(conn));
                server.ConnectionContext.ExecuteNonQuery(InstallScript.Install + addAccount + InstallScript.ELMAH);
            }
        }

        public static int Setup(string siteDomain)
        {
            int userProfileId = 0;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.CreateSite", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ApplicationName", siteDomain);
                    userProfileId = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }

            return userProfileId;
        }
    }
}